package com.guangzhi.weijianzhi.company.center;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.activity.BaseActivity;
import com.guangzhi.weijianzhi.context.WeiJianZhiEntity;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.utils.Misc;

public class EdittextActivity extends BaseActivity implements OnClickListener {
	
	private String mType;
	private EditText edit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mType = getIntent().getStringExtra("name");
		setContentView(R.layout.tz_edittext_layout);
		
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		edit = (EditText) findViewById(R.id.edit_id);
		Button button = (Button) findViewById(R.id.edit_bu);
		button.setOnClickListener(this);
		goBack.setOnClickListener(this);
		
		if("1".equals(mType)){
		  titleTextV.setText("公司名字");
		}else if("2".equals(mType)){
			titleTextV.setText("详细地址");
		}else if("3".equals(mType)){
			titleTextV.setText("公司简介");
		}else if("4".equals(mType)){
			titleTextV.setText("意见反馈");
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.edit_bu:
			
			String string = edit.getText().toString();
			
			
			if(Misc.notNull(string)){
				
				if("1".equals(mType)){
					 if(string.length()>25){
						 Misc.alert("公司名称不能大于25字");
					 }else{
							Intent intent = new Intent();
							intent.putExtra("resust",string); 
							setResult(RESULT_OK, intent);
							finish();
					 }
					}else if("2".equals(mType)){
						if(string.length()>20){
							 Misc.alert("详细地址不能大于20字");
						 }else{
								Intent intent = new Intent();
								intent.putExtra("resust",string); 
								setResult(RESULT_OK, intent);
								finish();
						 }
					}else if("3".equals(mType)){
						if(string.length()>200){
							 Misc.alert("公司简介不能大于200字");
						 }else{
								Intent intent = new Intent();
								intent.putExtra("resust",string); 
								setResult(RESULT_OK, intent);
								finish();
						 }
					}else if("4".equals(mType)){
						if(string.length()>500){
							Misc.alert("投诉建议不能大于200字");
						}else{
							submitData(string);
						}
						
						
					}
				
			}
			break;

		default:
			break;
		}
	}

	private void submitData(String str) {
		// TODO Auto-generated method stub
		HttpRequestUtils.doHttpSettingOpinion(WeiJianZhiEntity.access_token,str, new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					
					if(jsonO.optBoolean("status")){
						
						JSONArray optJSONArray = jsonO.optJSONArray("data");
						
						Misc.alert(optJSONArray.optString(0));
						finish();
						
					}else{
						Misc.alert(jsonO.optString("errMessage"));
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
			}
			
		});
	}

}
